package com.bw.good

import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.mvi.BaseMviActivity
import com.bawei.good.R
import com.bawei.good.databinding.ActivityBugGoodBinding
import com.bawei.image.LoadImageUtil
import com.bw.good.entity.GoodEntity
import dagger.hilt.android.AndroidEntryPoint

@Route(path = RoutePath.PATH_GOOD_BUG_PAGE)
@AndroidEntryPoint
class BugGoodActivity : BaseMviActivity<ActivityBugGoodBinding>() {

    @Autowired
    lateinit var goodEntity: GoodEntity

    private var goodNum: Int = 1

    override fun getLayoutId(): Int = R.layout.activity_bug_good

    override fun initViews() {

        ARouter.getInstance().inject(this)

        //返回点击事件
        mDataBinding.ivBack.setOnClickListener {
            this@BugGoodActivity.finish()
        }

        //商品信息
        mDataBinding.tvGoodDesc.text = goodEntity.goods_desc
        //商品价格
        mDataBinding.tvGoodPrice.text = "￥${goodEntity.goods_default_price}"
        //商品总价
        mDataBinding.tvSumPrice.text = "￥${goodEntity.goods_default_price}"
        //商品图片
        LoadImageUtil.loadUrl(this,goodEntity.goods_default_icon,mDataBinding.ivGoodIcon)
        //增加点击事件
        mDataBinding.tvAdd.setOnClickListener {
            getAdd()
        }
        //减少点击事件
        mDataBinding.tvJian.setOnClickListener {
            getJian()
        }

        //点击跳转
        mDataBinding.tvDingdan.setOnClickListener {
            startActivity(Intent(this,ZFActivity::class.java))
            this@BugGoodActivity.finish()
        }

    }

    //减少商品数量
    fun getJian() {
        if (goodNum > 1) {
            goodNum--
            mDataBinding.tvGoodNum.text = goodNum.toString()
        } else {
            showToastMsg("商品数量不能小于1")
        }
        getPrice()
    }

    //增加商品数量
    fun getAdd() {
        if (goodNum > goodEntity.goods_stock_count) {
            showToastMsg("库存不足")
        } else {
            goodNum++
            mDataBinding.tvGoodNum.text = goodNum.toString()
        }
        getPrice()
    }

    //计算总价
    fun getPrice() {
        var sum = goodNum * goodEntity.goods_default_price
        mDataBinding.tvSumPrice.text = "￥${sum}"
    }


}